var flag = false;
var PaperModule = function () {
    var editable = function () {
        return {
            init: function () {
                var oTable = $('#modelEditable').dataTable({
                    "lengthMenu": [
                        [10, 15, 20, 50],
                        [10, 15, 20, 50]
                    ],
                    "autoWidth":false,
                    "displayLength": 10,
                    "scrollX": true,
                    "language": {
                        "lengthMenu": "每页 _MENU_ 条记录",
                        "paginate": {
                            "previous": "上一页",
                            "next": "下一页"
                        },
                        "info" : "从 _START_ 到  _END_ 条，总记录为 _TOTAL_ 条",    
                        "infoEmpty" : "记录数为0",
                    },
                    "columnDefs": [
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "<center>-</center>",
						    "targets": [0]
						},
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "<center>-</center>",
						    "targets": [1]
						}, 
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "<center>-</center>",
						    "targets": [2]
						}, 
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "<center>-</center>",
						    "targets": [3]
						}
                    ],
                    "columns": [
                       { "data": "title" },
                       { "data": "current" },
                       { "data": "qualifiedLine" },
                       { "data": "operate" }
                   ],
                   "filter" : false,
                   "processing": false,
                   "serverSide": true,
                   "ajaxSource": ctx + "/question/paperList",
                   "serverMethod": "post",
                   "fnRowCallback" : function(nRow, aData, displayIndex) {
                	   var current = aData.current;
                	   nRow.children[0].id = aData.id;
                	   nRow.children[0].qualified = aData.qualified;
                	   nRow.children[0].current = current;
                	   if (current == true) {
                		   $('td:eq(1)', nRow).html('启用');
                		   $('td:eq(3)', nRow).html('<a class="detail fa fa-bars fa-lg " title="详情" href="javascript:;"></a>&nbsp;&nbsp;&nbsp;<a class="setting fa fa-cog fa-lg " title="设置合格线" href="javascript:;"></a>&nbsp;&nbsp;&nbsp;<a class="state fa fa-ban fa-lg " title="禁用" href="javascript:;"></a>&nbsp;&nbsp;&nbsp;<a class="delete fa fa-trash-o fa-lg " title="删除" href="javascript:;"></a>');
                           
                	   }else{
                		   $('td:eq(1)', nRow).html('禁用');
                		   $('td:eq(3)', nRow).html('<a class="detail fa fa-bars fa-lg " title="详情" href="javascript:;"></a>&nbsp;&nbsp;&nbsp;<a class="setting fa fa-cog fa-lg " title="设置合格线" href="javascript:;"></a>&nbsp;&nbsp;&nbsp;<a class="state fa fa-plus-circle fa-lg " title="启用" href="javascript:;"></a>&nbsp;&nbsp;&nbsp;<a class="delete fa fa-trash-o fa-lg " title="删除" href="javascript:;"></a>');
                	   }
                	   var qualified = "-";
                	   if(aData.qualified != undefined){
                		   qualified = aData.qualified;
                	   }
                	   $('td:eq(2)', nRow).html(qualified+" 分");
                	   return nRow;
                   },
                   "fnServerData" : function(ajaxSource, dataSet, fnCallback) {
                	   var json = "";
	                   var s;
	                   for (var i = 0; i < dataSet.length; i++) {
	                       s = dataSet[i];
	                   	   if (s.name == 'sEcho') {
	                   		   json = json + '"echo":' + s.value;
		                   } else if (s.name == 'iDisplayStart') {
		                	   json = json + ',"page":' + s.value;
		                   } else if (s.name == 'iDisplayLength') {
		                	   json = json + ',"pageSize":' + s.value;
		                   }
	                   }
                       $.ajax({
                       	   "contentType":"application/json; charset=UTF-8",
                           "dataType" : 'json',
                           "type" : "POST",
                           "url" : ctx + "/question/paperList",
                           "data" : json,
                           "success" : fnCallback
                       });
                   },
                });
               
                // 试卷详情
                $('#modelEditable').on("click", "a.detail", (function (e) {
                    e.preventDefault();
                    var nRow = $(this).parents('tr')[0];
                    var id = nRow.children[0].id;
                    window.location.href = ctx +"/question/paperDetail/"+id;
                    return;
                }));
                
                // 设置合格线
                $('#modelEditable').on("click", "a.setting", (function (e) {
                	var nRow = $(this).parents('tr')[0];
                	var id = nRow.children[0].id;
                	var qualified = nRow.children[0].qualified;
                	if(qualified != undefined){
                		document.getElementById('qualified_line').value = qualified;
                	}
                    document.getElementById('paper_id').value=id;
                	document.getElementById('setting_error_alert').style.display='none';
                	$("#btn_setting_Modal").click();
                }));
                
                // 试卷状态管理
                $('#modelEditable').on("click", "a.state", (function (e) {
                	var nRow = $(this).parents('tr')[0];
                	var current = nRow.children[0].current;
                	var id = nRow.children[0].id;
                	var content = "";
                	if(current == true){
                		content = "确认禁用本道试卷吗？禁用本道试卷将没有试卷启用,请重新设置启用试卷！";
                	}else{
                		content = "确认启用本道试卷吗？启用本试卷将导致当前启用试卷被禁用！";
                	}
                	
                	$.teninedialog({
                        title:'系统提示',
                        content:content,
                        showCloseButton:false,
                        otherButtons:["确定","取消"],
                        otherButtonStyles:['btn-primary','btn-white'],
                        bootstrapModalOption:{keyboard: true},
                        clickButton:function(sender,modal,index){
                       	 if(index == 0){
                       		 json = "{\"id\":\""+id+"\",\"current\":\""+current+"\"}";
                       		 $.ajax({
                                	type:"post",
                                    contentType:"application/json; charset=UTF-8",
                                    url: ctx + "/question/paperState",
                                    data:json,
                                    dataType:"json",
                                    success: function(msg){
                                   	$.teninedialog({
                                            title:'系统提示',
                                            content:msg.message,
                                            showCloseButton:false,
                                            otherButtons:["确定"],
                                            otherButtonStyles:['btn-primary'],
                                            bootstrapModalOption:{keyboard: true},
                                            clickButton:function(sender,modal,index){
                                            	location.reload();
                                            }
                                        });
                                    },
                                    error: function (msg) {
                                    	bootbox.alert("服务器出错了,修改试卷状态失败了!");
                                    }
                                });
                       	 }
                       	 $(this).closeDialog(modal); 
                        }
                    });
                }));
                
                // 删除试卷
                $('#modelEditable').on("click", "a.delete", (function (e) {
                	var nRow = $(this).parents('tr')[0];
                	var id = nRow.children[0].id;
                	var json = "{\"paperId\":\""+id+"\"}";
                	$.teninedialog({
                        title:'系统提示',
                        content:"确认要删除该问卷吗？",
                        showCloseButton:false,
                        otherButtons:["确定","取消"],
                        otherButtonStyles:['btn-primary','btn-white'],
                        bootstrapModalOption:{keyboard: true},
                        clickButton:function(sender,modal,index){
                       	 if(index == 0){
                       		$.ajax({
                            	type:"post",
                                contentType:"application/json; charset=UTF-8",
                                url: ctx + "/paper/isPaperUsed",
                                data:json,
                                dataType:"json",
                                success: function(msg){
                                	var code = msg.code;
                                	if(code == 1){
                                		var data = msg.data;
                                		if(data > 0){
                                			showError("该试卷已经被使用,无法删除!",1500,true);
                                		}else{
                                			deletePaper(id,nRow);
                                		}
                                	}else{
                                		bootbox.alert("服务器出错了,删除试卷失败了!");
                                	}
                                },
                                error: function (msg) {
                                	bootbox.alert("服务器出错了,删除试卷失败了!");
                                }
                            });
                       	 }
                       	 $(this).closeDialog(modal); 
                        }
                    });
                }));
                
                $('#btn_query').click(function(){
                	oTable.fnClearTable();
                });
            }

        };
    }();
    return {
        init: function () {
        	editable.init();
        }
    };
}();

function deletePaper(id,nRow){
	var json = "{\"id\":\""+id+"\"}"; 
	$.ajax({
     	type:"post",
         contentType:"application/json; charset=UTF-8",
         url: ctx + "/paper/deletePaper",
         data:json,
         dataType:"json",
         success: function(msg){
        	 if(msg.code == 1){
        		 showTips("删除试卷成功.",500,true);
        	 }else{
        		 showError("服务器出错了,删除试卷失败了!",2000,true); 
        	 }
         },
         error: function (msg) {
        	 showError("服务器出错了,删除试卷失败了!",2000,true);
         }
     });
}
var error = $('.alert-danger');
error.hide();
$('#btn_setting_qualified').click(function(){
	var qualified = $('#qualified_line').val();
	var id = $('#paper_id').val();
	if(qualified <0){
		document.getElementById('setting_error_alert').innerHTML = "合格线分数必须大于0";
		error.show();
		return;
	}
	json = "{\"id\":\""+id+"\",\"qualified\":\""+qualified+"\"}";
	$('#btn_setting_qualified').attr('disabled',true);
    var str = json;
    $.teninedialog({
        title:'系统提示',
        content:"确定修改该试卷合格线吗？修改合格线将会影响使用该试卷进行测评的投资人的测评结果！",
        showCloseButton:false,
        otherButtons:["确定","取消"],
        otherButtonStyles:['btn-primary','btn-white'],
        bootstrapModalOption:{keyboard: true},
        clickButton:function(sender,modal,index){
       	if(index == 0){
       		 $.ajax({
       	    	type:"post",
       	        contentType:"application/json; charset=UTF-8",
       	        url: ctx + "/question/modifyLine",
       	        data:json,
       	        dataType:"json",
       	        success: function(msg){
       	        	$('#btn_setting_qualified').attr('disabled',false);
       	        	if(msg.code == 1){
       	        		$('#dsimiss_modify_relation').click();
       	        		$.teninedialog({
       	                    title:'系统提示',
       	                    content:msg.message,
       	                    showCloseButton:false,
       	                    otherButtons:["确定"],
       	                    otherButtonStyles:['btn-primary'],
       	                    bootstrapModalOption:{keyboard: true},
       	                    clickButton:function(sender,modal,index){
       	                    	location.reload();
       	                    }
       	                });
       	        	}else{
       	        		bootbox.alert("服务器出错了,修改试卷合格线失败了!");
       	        	}
       	        },
       	        error: function (msg) {
       	        	$('#btn_setting_qualified').attr('disabled',false);
       	        	bootbox.alert("服务器出错了,修改试卷合格线失败了!");
       	        }
       	    });
       	 }else{
       		$('#btn_setting_qualified').attr('disabled',false);
       	 }
       	 $(this).closeDialog(modal); 
        }
    });
   
	
});